/**
 * File ChatNiReceiver.java 
 */
package cs.ni;

import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.util.Observer;

/**
 * Listens for incoming messages/connection requests.<p>
 * Package  cs.ni
 * Project ChatSystemG1 - INSA 4INFOA1 - 2013/2014
 * @author Julie DUMONS & Juste OUEDRAOGO
 */
public class ChatNiReceiver {
	
	
	/**
	 * udp receiver thread
	 * listens for incoming messages on a specific udp port
	 */
	private Thread udpReceiverThread;
	
	/**
	 * tcp server thread for file downloads
	 */
	private Thread tcpFileServerThread;
	/**
	 * observer of the message reception (char ni)
	 */
	private Observer observer;
	
	/**
	 * constructor
	 */
	public ChatNiReceiver(Observer observer, DatagramSocket udpSocket, ServerSocket tcpSocket) {
		//start listening for incoming messages 
		UdpReceiver udpRecv = new UdpReceiver(udpSocket);
		udpRecv.addObserver(observer);
		udpReceiverThread = new Thread(udpRecv);
		udpReceiverThread.start();
		
		//start tcp server thread
		FileReceiverServer server = new FileReceiverServer(tcpSocket, observer);
		tcpFileServerThread = new Thread(server);
		tcpFileServerThread.start();
	}
	
	/**
	 * call the thread of UdpReceiver and tcpFileServer and interrupt them
	 */
	public void stopReceiving(){
		udpReceiverThread.interrupt();
		tcpFileServerThread.interrupt();
	}
}
